package com.Array;

//冒泡排序
public class ArrayDemo {
    public static String arrayToString(int[] arr) {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1) {
                sb.append(arr[i]);
            } else {
                sb.append(arr[i] + ",");
            }
        }
        sb.append("]");
        String s = sb.toString();

        return s;
    }

    public static void main(String[] args) {
        int[] arr = {14, 56, 78, 89, 30, 52, 26};
        System.out.println("排序前:" + "\n" + arrayToString(arr));
        for (int a = 0; a < arr.length - 1; a++) {
            /*for(int b =0;b<arr.length-1;b++){
            //arr[b+1]可以取到arr[7]
                if(arr[b]>arr[b+1]){
                    int temp = arr[b];
                    arr[b] = arr[b+1];
                    arr[b+1] = temp;
                }
            }*/
            for (int b = 0; b < arr.length - 1 - a; b++) {
                if (arr[b] > arr[b + 1]) {
                    int temp = arr[b];
                    arr[b] = arr[b + 1];
                    arr[b + 1] = temp;
                }
            }
        }
        System.out.println("排序后:" + "\n" + arrayToString(arr));
    }
}

